#include "UdpServer.hpp"
#include "User.hpp"

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cerr << "Usage: " << argv[0] << "localport: " << std::endl;
        Die(USAGE_ERR);
    }

    uint16_t port = std::stoi(argv[1]); // √

    ENABLE_CONSOLE_LOG();

    // 用户管理模块
    std::shared_ptr<UserManager> um = std::make_shared<UserManager>();

    // 错误写法  类型不一致，AddUser中有隐藏参数this，不同于std::function<void (const InetAddr &id)>;
    // std::unique_ptr<UdpServer> svr_uptr = std::make_unique<UdpServer>(um->AddrUser(), port);

    // 正确写法：再构造一个函数将其调用过程包装起来
    std::unique_ptr<UdpServer> svr_uptr = std::make_unique<UdpServer>(port);
    svr_uptr->RegisterService([&um](InetAddr &id)
                            { 
                                um->AddrUser(id); 
                            }, 
                              [&um](int sockfd, const std::string &message)
                            { 
                                um->Router(sockfd, message); 
                            });
    svr_uptr->InitServer();
    svr_uptr->Start();

    return 0;
}
